package com.juyu.miliao.api.service.whitepacket;

import com.juyu.miliao.api.dao.mysql.WhitePacketCommentReplyInfoDao;
import com.juyu.miliao.api.dao.mysql.WhitePacketDynamicCommentInfoDao;
import com.juyu.miliao.api.dao.mysql.WhitePacketDynamicInfoDao;
import com.juyu.miliao.api.dao.redis.DynamicCommentInfoRedisDao;
import com.juyu.miliao.api.dao.redis.DynamicInfoRedisDao;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.CommentReplyInfo;
import com.juyu.miliao.api.domain.DynamicCommentInfo;
import com.juyu.miliao.api.domain.WhitePacketCommentReplyInfo;
import com.juyu.miliao.api.domain.WhitePacketDynamicCommentInfo;
import com.juyu.miliao.api.domain.dto.DynamicCommentInfoDTO;
import com.juyu.miliao.api.domain.dto.WhitePacketDynamicCommentInfoDTO;
import com.juyu.miliao.api.domain.vo.*;
import com.juyu.miliao.api.util.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuanqicheng
 * @create 2019-21-12:57
 */
@Service
public class WhitePacketDynamicCommentInfoService extends BasicWhitePacketService {

    @Autowired
    private WhitePacketDynamicCommentInfoDao whitePacketDynamicCommentInfoDao;

    @Autowired
    private WhitePacketDynamicInfoDao whitePacketDynamicInfoDao;

    @Autowired
    private WhitePacketCommentReplyInfoDao whitePacketCommentReplyInfoDao;
    @Autowired
    private UserInfoCache userInfoCache;


    /**
     * 添加评论信息
     * @param dynamicCommentInfoDTO
     */
    public void addDynamicComment(WhitePacketDynamicCommentInfoDTO dynamicCommentInfoDTO) {

        WhitePacketDynamicCommentInfo whitePacketDynamicCommentInfo = new WhitePacketDynamicCommentInfo();

        BeanUtils.copyProperties(dynamicCommentInfoDTO, whitePacketDynamicCommentInfo);

        whitePacketDynamicCommentInfoDao.addDynamicComment(whitePacketDynamicCommentInfo);
    }


    public Object getDynamicCommentPageByDynamicId(long dynamicId, int page, int pageSize) {

        Map<String,Object> resultMap = new HashMap<>(2);

        int startNum = PageUtil.getOffset(page, pageSize);

        WhitePacketDynamicDetailInfoVo dynamicDetailInfoVo = whitePacketDynamicInfoDao.getDynamicInfoById(dynamicId);

        if(dynamicDetailInfoVo!=null){
            List<WhitePacketDynamicCommentInfo> dynamicCommentInfoList = whitePacketDynamicInfoDao
                    .getDynamicCommentPageByDynamicId(dynamicId, startNum, pageSize);

            List<WhitePacketDynamicCommentInfoVo> dynamicCommentInfoVoList = new ArrayList<>();

            if(dynamicCommentInfoList.size()>0){

                for(WhitePacketDynamicCommentInfo obj : dynamicCommentInfoList){

                    WhitePacketDynamicCommentInfoVo dynamicCommentInfoVo = new WhitePacketDynamicCommentInfoVo();

                    setDynamicCommentInfoVo(dynamicCommentInfoVo, obj);

                    if(dynamicDetailInfoVo.getUserId().equals(dynamicCommentInfoVo.getFromUserId())){
                        dynamicCommentInfoVo.setIsAuthor(1);
                    }
                    List<WhitePacketCommentReplyInfo> commentReplyInfoList = whitePacketCommentReplyInfoDao
                            .getCommentReplyInfoByCommentId(obj.getId());

                    if (commentReplyInfoList.size() > 0) {

                        Map<String, Object> map = setCommentReplyInfo(commentReplyInfoList.size(), commentReplyInfoList.get(0));

                        dynamicCommentInfoVo.setReplyInfo(map);

                        dynamicCommentInfoVoList.add(dynamicCommentInfoVo);
                    }else{
                        dynamicCommentInfoVoList.add(dynamicCommentInfoVo);
                    }

                }

            }

            int total = whitePacketDynamicCommentInfoDao.getDynamicCommentCountByDynamicId(dynamicId);

            resultMap.put("list",dynamicCommentInfoVoList);

            resultMap.put("total",total);
            return resultMap;
        }else{
            resultMap.put("list",new ArrayList<>());
            resultMap.put("total",0);
            return resultMap;
        }
    }

    public Object getDetailInfo(long id) {

        WhitePacketDynamicCommentInfo dynamicCommentInfo =whitePacketDynamicCommentInfoDao.getDynamciCommentById(id);

        if(dynamicCommentInfo!=null){

            WhitePacketDynamicDetailInfoVo dynamicDetailInfoVo = whitePacketDynamicInfoDao
                    .getDynamicInfoById(dynamicCommentInfo.getDynamicId());

            WhitePacketDynamicCommentInfoVo dynamicCommentInfoVo = new WhitePacketDynamicCommentInfoVo();

            BeanUtils.copyProperties(dynamicCommentInfo, dynamicCommentInfoVo);

            UserCacheVo commentUserInfo = userInfoCache.getUserCacheInfo(dynamicCommentInfo.getFromUserId());

            int nowYear=LocalDateTime.now().getYear();

            int commentYear = dynamicCommentInfoVo.getCommentTime().getYear();

            String times;

            if(nowYear==commentYear){

                LocalDateTime time = dynamicCommentInfoVo.getCommentTime();

                times = time.getMonthValue()+"-"+time.format(DateTimeFormatter.ofPattern("dd HH:mm"));
            }else{
                times = dynamicCommentInfoVo.getCommentTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
            }

            dynamicCommentInfoVo.setFromUserNickName(commentUserInfo.getNickName())
                    .setFromUserIcon(commentUserInfo.getIcon())
                    .setFromUserAge(commentUserInfo.getAge())
                    .setFromUserIsHost(commentUserInfo.getIsHost())
                    .setFromUserIsVip(commentUserInfo.getIsVip())
                    .setFromUserVGrade(commentUserInfo.getVGrade())
                    .setFromUserMGrade(commentUserInfo.getMGrade())
                    .setFromUserSex(commentUserInfo.getSex())
                    .setShowTime(times);

            dynamicCommentInfoVo.setCommentId(dynamicCommentInfo.getId());

            if(dynamicCommentInfoVo.getFromUserId().equals(dynamicDetailInfoVo.getUserId())){
                dynamicCommentInfoVo.setIsAuthor(1);
            }

            List<WhitePacketCommentReplyInfo> commentReplyInfoList = whitePacketCommentReplyInfoDao
                    .getCommentReplyInfoByCommentId(dynamicCommentInfo.getId());
            List<CommentReplyDetailInfo> detailInfoList = new ArrayList<>();

            for(WhitePacketCommentReplyInfo obj : commentReplyInfoList){
                CommentReplyDetailInfo detailInfo = new CommentReplyDetailInfo();

                BeanUtils.copyProperties(obj,detailInfo);

                UserCacheVo userCacheVo= userInfoCache.getUserCacheInfo(obj.getReplyFromUserId());


                int year = obj.getReplyTime().getYear();

                String showTime;

                if(nowYear==year){
                    LocalDateTime time = obj.getReplyTime();
                    showTime = time.getMonthValue()+"-"+time.format(DateTimeFormatter.ofPattern("dd HH:mm"));
                }else{
                    showTime = obj.getReplyTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
                }
                detailInfo.setReplyFromUserNickName(userCacheVo.getNickName())
                        .setReplyFromUserIcon(userCacheVo.getIcon())
                        .setReplyFromUserAge(userCacheVo.getAge())
                        .setReplyFromUserIsHost(userCacheVo.getIsHost())
                        .setReplyFromUserIsVip(userCacheVo.getIsVip())
                        .setReplyFromUserSex(userCacheVo.getSex())
                        .setReplyFromUserMGrade(userCacheVo.getMGrade())
                        .setReplyFromUserVGrade(userCacheVo.getVGrade())
                        .setShowTime(showTime);

                if(detailInfo.getReplyFromUserId().equals(dynamicDetailInfoVo.getUserId())){

                    detailInfo.setIsAuthor(1);
                }
                detailInfoList.add(detailInfo);
            }
            dynamicCommentInfoVo.setReplyInfo(detailInfoList);
            return dynamicCommentInfoVo;
        }else{
            return new HashMap<>(0);
        }

    }
}
